/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.voto.sunflower.activity.me;

import retrofit.RetrofitError;
import retrofit.client.Response;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.voto.sunflower.R;
import com.voto.sunflower.activity.BaseActivity;
import com.voto.sunflower.model.request.ModifyPassword;
import com.voto.sunflower.model.response.ResultResponse;
import com.voto.sunflower.retrofit.ClientHttpService;
import com.voto.sunflower.retrofit.NetworkHandler;
import com.voto.sunflower.retrofit.RestError;

/**
 * 注册页
 * 
 */
public class ModifyPasswordActivity extends BaseActivity implements OnClickListener {
	
	public final static String TAG = "ModifyPasswordActivity";
	private boolean isHide = true;
	
	private EditText editOldPassword;
	private EditText editNewPassword;
	private EditText editPassword;

	private TextView back;
	private TextView title;
	private ImageView passwordCtr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_password);
		
		initComponent();

		// passwordCtr = (ImageView) findViewById(R.id.passwd_ctr);
		initTitle();

	}

	private void initComponent() {
		editOldPassword = (EditText) findViewById(R.id.editOldPassword);
		editNewPassword = (EditText) findViewById(R.id.editNewPassword);
		editPassword = (EditText) findViewById(R.id.editPassword);		
		
//		editOldPassword.setSelection(editOldPassword.getHint().length());
//		editNewPassword.setSelection(editNewPassword.getHint().length());
//		editPassword.setSelection(editPassword.getHint().length());
	}

	private void initTitle() {
		back = (TextView) findViewById(R.id.back);
		back.setOnClickListener(this);
		title = (TextView) findViewById(R.id.title);
		title.setText(R.string.edit_password);
		TextView other = (TextView)findViewById(R.id.other);
		other.setVisibility(View.VISIBLE);
		other.setText(getResources().getString(R.string.save));
		other.setOnClickListener(this);
	}

	/**
	 * 失去焦点
	 */
	public void loseFocus(View view) {
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
		InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editPassword.getWindowToken(), 0);
	}

	/**
	 * 改变密码状态
	 * 
	 * @param view
	 */
	public void passwdStateChange(View view) {
		if (isHide) {
			isHide = false;
			passwordCtr.setBackgroundResource(R.drawable.show_password);
			editPassword.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
		} else {
			isHide = true;
			passwordCtr.setBackgroundResource(R.drawable.hide_password);
			editPassword.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
		}
	}

	/**
	 * 完成
	 * 
	 * @param view
	 */
	public void modifyPasswordComplete() {
		// 验证是否为空
		String oldPassword = editOldPassword.getText().toString();
		String newPassword = editNewPassword.getText().toString();
		String repeatPassword = editPassword.getText().toString();
		if (TextUtils.isEmpty(oldPassword)) {
			editOldPassword.setError("密码不能为空");
			editOldPassword.requestFocus();
			return;
		} else if (TextUtils.isEmpty(newPassword)) {
			editNewPassword.setError("密码不能为空");
			editNewPassword.requestFocus();
			return;
		} else if (TextUtils.isEmpty(repeatPassword)) {
			editPassword.setError("密码不能为空");
			editPassword.requestFocus();
			return;
		} else if (oldPassword.length() < 6 || oldPassword.length() < 6 || oldPassword.length() < 6) {
			Toast.makeText(ModifyPasswordActivity.this, "密码不能少于6位数", Toast.LENGTH_SHORT).show();
			return;
		} else if (!editNewPassword.getText().toString().equals(editPassword.getText().toString())) {
			Toast.makeText(ModifyPasswordActivity.this, R.string.edit_password_not, Toast.LENGTH_SHORT).show();
			return;
		}
		// 发送修改密码
		showBlankWaitDialog();
		ModifyPassword modifyPassword = new ModifyPassword();
		modifyPassword.setOld_password(editOldPassword.getText().toString().trim());
		modifyPassword.setPassword(editPassword.getText().toString().trim());
		ClientHttpService.getMeService().updatePasswd(modifyPassword, new UpdatePasswordCallback());
	}

	class UpdatePasswordCallback extends NetworkHandler<ResultResponse> {
		@Override
		public void failure(RetrofitError error) {
			super.failure(error);
			dismissDialog();
			if (error.getResponse() != null) {
				int statusCode = error.getResponse().getStatus();
				if (statusCode == 500 || statusCode == 501) {
					final RestError body = (RestError) error.getBodyAs(RestError.class);
					showNetworkErrorDialog(body.errorDetails, null);
				} else if (statusCode == 408) {
					showNetworkErrorDialog(getString(R.string.connect_overtime), null);
				} else {
					showNetworkErrorDialog(error.getMessage(), null);
				}
			}
		}

		@Override
		public void success(ResultResponse arg0, Response arg1) {
			super.success(arg0, arg1);
			dismissDialog();
			Toast.makeText(ModifyPasswordActivity.this, R.string.edit_password_success, Toast.LENGTH_SHORT).show();
			back();
		}
	}

	public void back() {
		InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editPassword.getWindowToken(), 0);
		finish();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			back();
			break;
		case R.id.passwd_ctr:
			if (isHide) {
				isHide = false;
				passwordCtr.setBackgroundResource(R.drawable.hide_password);
				editPassword.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			} else {
				isHide = true;
				passwordCtr.setBackgroundResource(R.drawable.show_password);
				editPassword.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			}
			break;
		case R.id.other:
			modifyPasswordComplete();
			break;
		default:
			break;
		}
	}

}
